Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structured partial object "shallow copy" and co-aplied destructuring #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

wentout
Copy link

@wentout wentout commented May 22, 2019

Idea is about to extract only some necessary props to the other object directly, wich means without any intermediate actions.

const myPreCondition = {
	a : { d : 1 },
	b : { e : 2, f : 3, g: 7 },
	c : 2
};

// then we might be able
const extra = myPreCondition.{ a, c };

// having the following
console.log(Object.keys(extra)); // ['a', 'c']
console.log(extra); // { a : { d : 1 }, c : 2 }

This means we did Object.assign directly, without any intermediate memory consumption, just made key a in extra as pointer to a of myPreCondition and made key c as immediately destructured 2 as it was pointed to numeric vector value.

In an addition:

const deepExtra = myPreCondition.{ a, c, b.{e} },
console.log(Object.keys(deepExtra)); // ['a', 'c', 'e']

const otherExtra = myPreCondition.{ a, b.{ ... } },
console.log(Object.keys(deepExtra)); // ['a', 'e', 'f', 'g']
console.log(deepExtra); // { a : { d : 1}, e : 2, f : 3, g : 7 } 

const existentObject = { e : 1, f : 2 };

// so here is an **assignment**
existentObject.{ ...myPreCondition.{ c, b.{e, f} } };

console.log(Object.keys(existentObject)); // ['c', 'e', 'f']
console.log(existentObject.e); // 2, as it was at myPreCondition.b.e;
console.log(existentObject.f); // 3, as it was at myPreCondition.b.f;

For sure can be optimized under the hood of V8.

@wentout wentout changed the title structured partial object "shallow copy" and co-aplied destruction structured partial object "shallow copy" and co-aplied destructuring May 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant